package com.vdg.lockapp;

import com.vdg.data.SettingData;
import com.vdg.lockapp.PassCodeActivity.PASS_CODE_ACTION_TYPE;
import com.vdg.lockapp.service.LockService;
import com.vdg.lockapp.service.LockService.LocalBinder;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;

public class SplashActivity extends Activity {
	private static final int TIME_SPLASH = 3000;
	private Handler mHandler;
	private SettingData mSettingData;
	private static final int SETUP_PASSWORD_REQUEST_CODE = 1;
	private static final int UNLOCK_REQUEST_CODE = 2;
	private LockService mService;
	Runnable mStartRunable = new Runnable() {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			start();
		}
	};
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		// required for Sherlock's invalidateOptionsMenu */
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mService = binder.getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}
	};
	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_layout);
		Intent serviceIntent = new Intent(this, LockService.class);
		if (!isMyServiceRunning()) {
			startService(serviceIntent);
		}
		bindService(serviceIntent, mServiceConnection, BIND_AUTO_CREATE);
		mSettingData = SettingData.getInstance(this);
		mHandler = new Handler();
		mHandler.postDelayed(mStartRunable, TIME_SPLASH);
	};

	public void start() {
		if (mSettingData.getPassCode() != null
				&& !mSettingData.getPassCode().equals("")) {
			if (mSettingData.getRequirePassword()) {
				Intent intent = new Intent(this, PassCodeActivity.class);
				intent.putExtra(PassCodeActivity.PASS_CODE_ACTION,
						PASS_CODE_ACTION_TYPE.UNLOCK_ACTION.toString());
				intent.putExtra(LockService.APP_LOCK_PACKAGE_NAME,
						getPackageName());
				startActivityForResult(intent, UNLOCK_REQUEST_CODE);
			} else {
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		} else {
			Intent intent = new Intent(this, PassCodeActivity.class);
			intent.putExtra(PassCodeActivity.PASS_CODE_ACTION,
					PASS_CODE_ACTION_TYPE.SETUP_PASSWORD_ACTION.toString());
			startActivityForResult(intent, SETUP_PASSWORD_REQUEST_CODE);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == SETUP_PASSWORD_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			} else {
				finish();
			}
		}
		if (requestCode == UNLOCK_REQUEST_CODE) {
			if (resultCode == RESULT_OK) {
				Intent intent = new Intent(this, MainActivity.class);
				startActivity(intent);
				finish();
			} else {
				finish();
			}
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mHandler.removeCallbacks(mStartRunable);
		unbindService(mServiceConnection);
		super.onDestroy();
	}

	private boolean isMyServiceRunning() {
		ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		for (RunningServiceInfo service : manager
				.getRunningServices(Integer.MAX_VALUE)) {
			if (LockService.class.getName().equals(
					service.service.getClassName())) {
				return true;
			}
		}
		return false;
	}
}
